package com.marry.webservice.config;

import com.marry.webservice.interceptor.AuthInterceptor;
import com.marry.webservice.webservice.LayoutImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import javax.xml.ws.Endpoint;

/**
 * @author ml
 * @create 2017-09-20--15:14
 */
@Configuration
public class CXFConfig {

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus(){
        return new SpringBus();
    }

    @Bean
    public LayoutImpl layout(){
        return new LayoutImpl();
    }

    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint=new EndpointImpl(springBus(),layout());
        endpoint.publish("/layout");
        endpoint.getInInterceptors().add(new AuthInterceptor());
        return endpoint;
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        ServletRegistrationBean bean=new ServletRegistrationBean(new CXFServlet(),"/webservices/*");
        bean.setLoadOnStartup(1);
        bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return bean;

    }

}
